package Classes.GameDatas// ActionScript file
{
	import Classes.GameErrors.GameError;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MapNPC;
	import Classes.GameViews.ViewDisplayers.VD_MapGrid;
	
	import flash.geom.Point;

	public class GD_MapGrid extends GameData
	{
		private var _map:GD_Map;/*所处地图*/
		private var _coordinate:int;/*坐标值*/
		private var _isHaven:Boolean;/*避难所标记*/
		private var _npc:MapNPC;/*该地图格上的NPC*/
		private var _building:MT_Building;/*该地图格上的建筑物*/
		private var _isWall:Boolean;/*是否拥有墙壁*/
		private var _isCharacter:Boolean;/*是否有角色*/
		private var _view:VD_MapGrid;/*地图格显示对象*/
		//private var _isInVision:Boolean;/*是否在视野内*/
		public function GD_MapGrid(coordinateValue:int,map:GD_Map):void
		{
			this._coordinate=coordinateValue;
			this._map=map;
		}
		
		/**
		 *创建地图格 
		 * @param coordinateValue 地图格坐标值
		 * @param mapWidthNum 地图宽度
		 * @return 地图格
		 * 
		 */				
		public static function creat(coordinate:int,map:GD_Map):GD_MapGrid
		{
			var mapGrid:GD_MapGrid=new GD_MapGrid(coordinate,map);
			return mapGrid;
		}
		
		/**
		 *地图格条件检测 
		 * @param conditionsAry 条件数组
		 * @return 是否满足条件
		 * 
		 */		
		public function conditionsCheck(conditionsAry:Array):Boolean
		{
			for each(var conditionStr:String in conditionsAry)
			{
				switch(conditionStr)
				{
					case "避难所内":
						if(!this.isHaven)return false;
						break;
					case "避难所外":
						if(this.isHaven)return false;
						break;
					case "没有NPC":
						if(this.npc)return false;
						break;
					case "没有建筑":
						if(this.building)return false;
						break;
					case "没有阻挡":
						if(this.building && this.building.buildingData && !this.building.buildingData.isFloor)return false;
						break;
					case "没有围墙":
						if(this.isWall)return false;
						break;
					case "没有角色":
						if(this.isCharacter)return false;
						break;
				}
			}
			return true;
		}
		
		/**
		 *获得到达某目标格的方向 
		 * @param toGrid 目标格
		 * @return 方向值
		 * 
		 */		
		public function getDir(toGrid:GD_MapGrid):int
		{
			var disX:int=toGrid.atx-this.atx;
			var disY:int=toGrid.aty-this.aty;
			if(disY==1)
			{
				if(disX==1)
				{
					return 7;
				}
				else if(disX==-1)
				{
					return 1;
				}
				else if(disX==0)
				{
					return 8;
				}
			}
			else if(disY==-1)
			{
				if(disX==1)
				{
					return 5;
				}
				else if(disX==-1)
				{
					return 3;
				}
				else if(disX==0)
				{
					return 4;
				}
			}
			else if(disY==0)
			{
				if(disX==1)
				{
					return 6;
				}
				else if(disX==-1)
				{
					return 2;
				}
			}
			return 0;
		}
		
		/**
		 *获取某方向上的相邻地图格 
		 * @param dir 方向
		 * @return 相邻地图格
		 * 
		 */		
		public function getNeighbourByDir(dir:int):GD_MapGrid
		{
			var nx:int=this.atx;
			var ny:int=this.aty;
			switch(dir)
			{
				case 1:
					nx-=1;
					ny+=1;
					break;
				case 2:
					nx-=1;
					break;
				case 3:
					nx-=1;
					ny-=1;
					break;
				case 4:
					ny-=1;
					break;
				case 5:
					nx+=1;
					ny-=1;
					break;
				case 6:
					nx+=1;
					break;
				case 7:
					nx+=1;
					ny+=1;
					break;
				case 8:
					ny+=1;
					break;
			}
			var targetGrid:GD_MapGrid;
			try
			{
				targetGrid=GV_Map.instance.getCoordinateGrid(new Point(nx,ny));
			}
			catch(e:GameError)
			{
				
			}
			return targetGrid;
		}
		
		override public function release():void
		{
			super.release();
			this._building=null;
			this._npc=null;
		}

		/*getter & setter*/
		public function get coordinate():int
		{
			return _coordinate
		}
		
		public function get atx():int
		{
			return this.coordinate%this.map.widthNum;
		}

		public function get aty():int
		{
			return this.coordinate/this.map.widthNum;
		}
		
		public function get coordinatePoint():Point
		{
			return new Point(this.atx,this.aty);
		}

		public function get isHaven():Boolean
		{
			return _isHaven;
		}

		public function set isHaven(value:Boolean):void
		{
			_isHaven = value;
		}

		public function get npc():MapNPC
		{
			return _npc;
		}

		public function set npc(value:MapNPC):void
		{
			_npc = value;
		}

		public function get building():MT_Building
		{
			return _building;
		}

		public function set building(value:MT_Building):void
		{
			_building = value;
		}

		public function get isWall():Boolean
		{
			return _isWall;
		}

		public function set isWall(value:Boolean):void
		{
			_isWall = value;
		}

		public function get isCharacter():Boolean
		{
			return _isCharacter;
		}

		public function set isCharacter(value:Boolean):void
		{
			_isCharacter = value;
		}

		public function get view():VD_MapGrid
		{
			if(!this._view)this._view=new VD_MapGrid(this);
			return _view;
		}

		public function set view(value:VD_MapGrid):void
		{
			_view = value;
		}
		
		public function get buildEnabled():Boolean
		{
			return (this.isHaven && !this.isWall && !this.building);
		}

		public function get map():GD_Map
		{
			return _map;
		}


	}
}